﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class CheckData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["userName"] == null || Session["roleID"] == null)
            {
                Response.Redirect("Login.aspx");
                Response.End();
            }
            string DataID = Request.QueryString["DataId"].ToString();
            //判断该用户是否有审核该流程的权限
            string userName = Session["userName"].ToString();
            string roleID = Session["roleID"].ToString();
            string LCID = "LC" + DataID;
            Boolean ismyFlow = true;
            if (roleID != "1")
            {
                CheckFlow CF = new CheckFlow();
                ismyFlow = CF.CheckIsMyFlow(userName, LCID);
            }
            if (ismyFlow == false)
            {
                Response.Write("<script language='javascript'>alert('对不起！你无权审核该文件！');location.href='CheckDataList.aspx'</script>");
                Response.End();

            }           
            
            //判断流程审核情况
            int FlowStatus = CheckStatus(DataID);
            if (FlowStatus==0)
            {
                Response.Redirect("CheckDataList.aspx", true);
            }
            else if (FlowStatus == 1)
            {
                BindDataFinish(DataID);
            }
            else
            {
                BindDataNext(DataID);
            }           
            
        }        

    }
    //向下传递
    protected void BindDataNext(string DataID)
    {
        string dataYear=DataID.Substring(2,4);
        string sqlString = "select LCBZ.AUTOID as LCBZAUTOID,* From LC" + dataYear + " as LC inner join LCBZ" + dataYear + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID where LC.DATAID='" + DataID + "' and LCBZ.STATUS=0 order by SEQ ASC";
        DBOperation DBO = new DBOperation();
        BasicClass BC=new BasicClass();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);        
        if (sdr.Read())
        {
            lblDataID.Text = DataID;
            lblStepID.Text = sdr["LCBZAUTOID"].ToString();
            rbtnNext.Checked = true;
            rbtnBack.Enabled = true;
            rbtnPass.Checked = true;
            rbtnNotPass.Checked = false;            
            sdr.Read();
            lblDep.Text = BC.GetDepNameByUserName(sdr["OPERATORID"].ToString());
            lblName.Text = BasicClass.GetTrueNameByUserName(sdr["OPERATORID"].ToString());
            lblToUserName.Text = sdr["OPERATORID"].ToString();
            lblToStepID.Text=sdr["LCBZAUTOID"].ToString();
            rbtnFinish.Enabled = false;            
            if (CheckFirstStep(DataID))
            {
                rbtnBack.Enabled = false;
            }

        }
        sdr.Close();
      
    }
    //完成
    protected void BindDataFinish(string DataID)
    {
        string dataYear = DataID.Substring(2, 4);
        string sqlString = "select top 1 LCBZ.AUTOID as LCBZAUTOID,* From LC" + dataYear + " as LC inner join LCBZ" + dataYear + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID where LC.DATAID='" + DataID + "' and LCBZ.STATUS=0 order by SEQ ASC";
        DBOperation DBO = new DBOperation();
        BasicClass BC = new BasicClass();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            lblDataID.Text = DataID;
            lblStepID.Text = sdr["LCBZAUTOID"].ToString();
            rbtnBack.Checked = false;
            rbtnBack.Enabled = true;
            rbtnNext.Checked = false;
            rbtnNext.Visible = false;
            rbtnFinish.Checked = true;           
            lblDep.Text = "无";
            lblName.Text = "无";
            lblToUserName.Text ="";
            if (CheckFirstStep(DataID))
            {
                rbtnBack.Enabled = false;
            }
        }
        sdr.Close();
 
    }
    //回退
    protected void BindDataBack(string DataID)
    {
        string dataYear = DataID.Substring(2, 4);
        string sqlString = "select top 1 LCBZ.AUTOID as LCBZAUTOID,* From LC" + dataYear + " as LC inner join LCBZ" + dataYear + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID where LC.DATAID='" + DataID + "' and LCBZ.STATUS=1 order by SEQ DESC";
        DBOperation DBO = new DBOperation();
        BasicClass BC = new BasicClass();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            rbtnPass.Checked = false;
            rbtnNotPass.Checked = true;
            lblToStepID.Text = sdr["LCBZAUTOID"].ToString();
            lblDep.Text = BC.GetDepNameByUserName(sdr["OPERATORID"].ToString());
            lblName.Text = BasicClass.GetTrueNameByUserName(sdr["OPERATORID"].ToString());
            lblToUserName.Text = sdr["OPERATORID"].ToString();
            if (CheckFirstStep(DataID))
            {
                rbtnBack.Enabled = false;
            }
        }
        sdr.Close();
 
    }
    //检测该流程还有多少个步骤没有完成，返回没有审核的步骤数目
    protected int CheckStatus(string DataID)
    {
        string dataYear=DataID.Substring(2,4);
        string sqlString = "select count(*) from LC" + dataYear + " as LC inner join LCBZ" + dataYear + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID where LC.STATUS=0 and LCBZ.STATUS=0 and LC.DATAID='" + DataID + "'";
        DBOperation DBO = new DBOperation();
        int NotCheckStep = Convert.ToInt32(DBO.ExcuteScalar(sqlString));
        return NotCheckStep;
    }
    //判断当前步骤是否为第一步,若是则返回TRUE，否则返回false
    protected Boolean CheckFirstStep(string DataID)
    {
        string dataYear = DataID.Substring(2, 4);
        string sqlString = "select count(*) from LC" + dataYear + " as LC inner join LCBZ" + dataYear + " as LCBZ on LC.TRUEFLOWID=LCBZ.TRUEFLOWID where LC.STATUS=0 and LCBZ.STATUS=1 and LC.DATAID='" + DataID + "'";
        DBOperation DBO = new DBOperation();
        int CheckStepNO = Convert.ToInt32(DBO.ExcuteScalar(sqlString));
        if (CheckStepNO == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    } 
   
    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!chbNoModify.Checked)
        {
            bool upFileStatus = UpDataFile();
            if (upFileStatus == true)
            {
                lblSuccessText.Text = "文件上传成功！";
                lblUpFileStatus.Text = "1";
            }
            else
            {
                lblSuccessText.Text = "处理失败，原因：文件上传失败！请重新处理！";              
                lblUpFileStatus.Text = "";
                return;
            }                       
             
        }
        string dataID = lblDataID.Text.ToString();
        string dataYear = dataID.Substring(2, 4);
        int autoID = Convert.ToInt32(lblStepID.Text.ToString().Trim());
        string strToAutoID = lblToStepID.Text.ToString().Trim();
        int toAutoID = 0;
        if (strToAutoID != "")
        {
           toAutoID = Convert.ToInt32(strToAutoID);
        }
        string attitude="";
        if (rbtnPass.Checked == true)
        {
            attitude = rbtnPass.Text.ToString();
        }
        else
        {
            attitude = "退回";
        }
        string CheckBrief = txtAnnotate.Text;
        string endDate = DateTime.Now.ToString();
        //回退提交
        if (rbtnBack.Checked == true)
        {
            //修改步骤状态
            string sqlString = "update LCBZ" + dataYear + " set ATTITUDE='" + attitude + "',ENDDATE='" + endDate + "',ANNOTATE='" + CheckBrief + "',STATUS=0 where AUTOID=" + autoID + "";
            string upadteToUserSqlString = "update LCBZ" + dataYear + " set ATTITUDE='文件退回',ENDDATE='" + endDate + "',ANNOTATE='[文件退回]" + CheckBrief + "',STATUS=0 where AUTOID=" + toAutoID + "";
            DBOperation DBO = new DBOperation();
            Boolean updateStaTus = DBO.ExcuteSql(sqlString) && DBO.ExcuteSql(upadteToUserSqlString);
            if (updateStaTus == true)
            {
                string sqlStringMoLC = "update LC" + dataYear + " set STATUS=0,CURRENTSTEP=CURRENTSTEP-1 where DATAID='" + dataID + "'";
                string ToURL = "CheckSuccess.aspx?ToUserName=" + lblToUserName.Text + "&Name=" + lblName.Text + "";
                if (DBO.ExcuteSql(sqlStringMoLC))
                {
                    //发送信息
                    Boolean sendMessageStatus = sendMessage(dataID);
                    if (sendMessageStatus == true)
                    {
                        Response.Redirect(ToURL);
                    }
                    else
                    {
                        Response.Write("<script language='JavaScript'>alert('发送信息失败！');</script>");
                        Response.Redirect(ToURL);
                    }
                }
            }
        }

        //向下传递
        if (rbtnNext.Checked == true)
        {
            //修改步骤状态
            string sqlString = "update LCBZ" + dataYear + " set ATTITUDE='" + attitude + "',ENDDATE='" + endDate + "',ANNOTATE='" + CheckBrief + "',STATUS=1 where AUTOID=" + autoID + "";
            DBOperation DBO = new DBOperation();
            if (DBO.ExcuteSql(sqlString))
            {
                string sqlStringMoLC = "update LC" + dataYear + " set STATUS=0,CURRENTSTEP=CURRENTSTEP+1 where DATAID='" + dataID + "'";
                string ToURL = "CheckSuccess.aspx?ToUserName=" + lblToUserName.Text + "&Name=" + lblName.Text + "";
                if (DBO.ExcuteSql(sqlStringMoLC))
                {
                    //发送信息
                    Boolean sendMessageStatus = sendMessage(dataID);
                    if (sendMessageStatus == true)
                    {
                        Response.Redirect(ToURL);
                    }
                    else
                    {
                        Response.Write("<script language='JavaScript'>alert('发送信息失败！');</script>");
                        Response.Redirect(ToURL);
                    }
                }

            }
        }
        //完成
        if (rbtnFinish.Checked == true)
        {
            //修改步骤状态
            string sqlString = "update LCBZ" + dataYear + " set ATTITUDE='" + attitude + "',ENDDATE='" + endDate + "',ANNOTATE='" + CheckBrief + "',STATUS='1' where AUTOID=" + autoID + "";
            DBOperation DBO = new DBOperation();
            if (DBO.ExcuteSql(sqlString))
            {
                //更改流程状态
                string sqlStringMoLC = "update LC" + dataYear + " set ENDDATE='" + endDate + "',STATUS=1,CURRENTSTEP=CURRENTSTEP+1 where DATAID='" + dataID + "'";
                string ToURL = "CheckSuccess.aspx?ToUserName=" + lblToUserName.Text + "&Name=" + lblName.Text + "";
                if (DBO.ExcuteSql(sqlStringMoLC))
                {
                    //发送信息
                    Boolean sendMessageStatus = sendMessage(dataID);
                    if (sendMessageStatus == true)
                    {
                        Response.Redirect(ToURL);
                    }
                    else
                    {
                        Response.Write("<script language='JavaScript'>alert('发送信息失败！');</script>");
                        Response.Redirect(ToURL);
                    }
                }
            }

        }
    }
    
    //发送信息
    protected Boolean sendMessage(string dataID)
    {
        Boolean success=true;
        try
        {
            CheckFlow CF = new CheckFlow();
            string fromUserName = Session["userName"].ToString();
            string toUserName = lblToUserName.Text.Trim();
            string content = txtAnnotate.Text;
            string flowId = "LC" + dataID;            
            CF.SendCheckMessage(flowId, fromUserName, toUserName, content);
            success = true;

        }
        catch
        {
            success = false;

        }       
        return success;
        
 
    }
    //单击回退时更改数据绑定
    protected void rbtnBack_CheckedChanged(object sender, EventArgs e)
    {
        string DataID = lblDataID.Text;
        if (!CheckFirstStep(DataID))
        {
            BindDataBack(DataID);
        }

    }
    //单击完成时更改数据绑定
    protected void rbtnFinish_CheckedChanged(object sender, EventArgs e)
    {
        string DataID = lblDataID.Text;
        BindDataFinish(DataID);
    }
    //单击向下传递时更新数据绑定
    protected void rbtnNext_CheckedChanged(object sender, EventArgs e)
    {
        string DataID = lblDataID.Text;
        BindDataNext(DataID);
    }
    //单击取消，清空批注中的文字
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MessageInfo.aspx");
    }
    protected void btnUpFile_Click(object sender, EventArgs e)
    {
        if (!chbNoModify.Checked)
        {
            bool upFileStatus=UpDataFile();
            if (upFileStatus == true)
            {
                lblSuccessText.Text = "文件上传成功！";
                lblUpFileStatus.Text = "1";
            }
            else
            {
                lblSuccessText.Text = "文件上传失败！";
                lblUpFileStatus.Text = "";
            }
            
        }

    }
    private Boolean UpDataFile()
    {        
        string fullFileName = FileUploadDaTa.PostedFile.FileName.ToString().Trim();
        Boolean upFileStatus = false;
        if (fullFileName == "")
        {
            upFileStatus = false;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('如果你修改了主文件请您先上传修改过的附件！否则请选择未修改！');", true);            
        }
        else
        {

            string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();
            string SaveFilename = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
            if (type == "exe")
            //if (type.ToString() != "jpg" && type != "jpeg" && type != "bmp" && type != "gif" && type != "png")
            {
                upFileStatus = false;
                Response.Write("<script language='javascript'>alert('文件格式不对！');</script>");                
            }
            else
            {
                DataOperation DataO = new DataOperation();
                string dataID = lblDataID.Text;
                string FilePath = DataO.GetFilesPathByDataID(dataID);
                FileUploadDaTa.PostedFile.SaveAs(Server.MapPath("files") + FilePath);
                upFileStatus = true;
            }
            
        }
        return upFileStatus;
    }
    protected void chbNoModify_CheckedChanged(object sender, EventArgs e)
    {
        if (chbNoModify.Checked)
        {
            FileUploadDaTa.Visible = false;
            btnUpFile.Visible = false;
        }
        else
        {
            FileUploadDaTa.Visible = true;
            btnUpFile.Visible = false;
        }
    }
}
